//
//  ViewController.m
//  VRDemo
//
//  Created by Agensn on 2017/3/9.
//  Copyright © 2017年 🌹Agensn_Master🌹. All rights reserved.
//

#import <CoreMotion/CoreMotion.h>

#import "ViewController.h"
#import "AppDelegate.h"
#import "DataBase.h"
#import "VRVideo.h"

#import "MyCollectionViewCell.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "PlayVRViewController.h"

#import "SRAlertView.h"

#import <Masonry.h>

@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,  SRAlertViewDelegate>
{
    NSMutableArray *theArr;
    UICollectionView *view;
}
@property (strong , nonatomic) CMMotionManager* motionManager;


@end

@implementation ViewController

-(void)viewWillAppear:(BOOL)animated{
    //在视图出现的时候，将allowRotate改为1，
    AppDelegate * delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    delegate.allowRotate = 1;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
     [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(VR:) name:@"VR" object:nil];
    [[DataBase shareData]jsonData ];
    
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    UILabel *titlab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 60)];
    titlab.text = @"VR极限运动";
    titlab.textColor = [UIColor whiteColor];
    titlab.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titlab;
    
    //创建流布局对象
    UICollectionViewFlowLayout *collection = [[UICollectionViewFlowLayout alloc]init];
    //设定单元格大小
    collection.itemSize = CGSizeMake(190, 150);
    //滚动方向
    collection.scrollDirection = UICollectionViewScrollDirectionVertical;
    //最小间距
    collection.minimumInteritemSpacing = 10;
    //边距
    collection.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    //创建网格对象
    view = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:collection];
    view.delegate = self;
    view.dataSource = self;
    //整夜滚动
    view.pagingEnabled = NO;
    //注册单元格
    [view registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"MyCollectionViewCell"];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    
   
}

-(void)VR:(NSNotification *)bb{
    theArr = bb.object;
    [view reloadData];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return theArr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCollectionViewCell" forIndexPath:indexPath];
    
    VRVideoClass *VR1 = theArr[indexPath.row];
    
    cell.mainLab.text = VR1.Name;
    cell.subLab.text = VR1.SubName;
    [cell.img sd_setImageWithURL:[NSURL URLWithString:VR1.Thumbnail]];
    
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    PlayVRViewController *vrp = [[PlayVRViewController alloc]init];
    VRVideoClass *VR1 = theArr[indexPath.row];
    vrp.Path1K = VR1.Path1K;
    vrp.Path2K = VR1.Path2K;
    vrp.Description = VR1.Description;
    vrp.Time = VR1.Time;
    
    //创建动画
    CATransition *animation = [CATransition animation];
    //设置运动轨迹的速度
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
    //设置动画类型为立方体动画
    animation.type = @"cube";
    //设置动画时长
    animation.duration =0.5f;
    //设置运动的方向
    animation.subtype =kCATransitionFromRight;
    //控制器间跳转动画
    [[UIApplication sharedApplication].keyWindow.layer addAnimation:animation forKey:nil];
    [self presentViewController:vrp animated:NO completion:nil];

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
